
package ch.hearc.meteo.imp.afficheur.real.vue.atome;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import ch.hearc.meteo.imp.afficheur.real.AfficheurService;
import ch.hearc.meteo.imp.afficheur.real.JDialogComOptions;
import ch.hearc.meteo.imp.afficheur.real.JDialogPreferencesLocal;
import ch.hearc.meteo.imp.afficheur.real.JFrameStationMeteo;
import ch.hearc.meteo.imp.afficheur.real.moo.StationMeteoElement;
import ch.hearc.meteo.imp.com.real.com.ComOption;
import ch.hearc.meteo.imp.reseau.AfficheurManager;
import ch.hearc.meteo.imp.use.remote.PCLocal;
import ch.hearc.meteo.spec.afficheur.AffichageOptions;
import ch.hearc.meteo.spec.afficheur.AfficheurFactory;
import ch.hearc.meteo.spec.meteo.MeteoServiceFactory;
import ch.hearc.meteo.spec.meteo.MeteoServiceOptions;
import ch.hearc.meteo.spec.meteo.MeteoService_I;
import ch.hearc.meteo.spec.meteo.exception.MeteoServiceException;
import ch.hearc.meteo.spec.meteo.listener.MeteoListener_I;
import ch.hearc.meteo.spec.meteo.listener.event.MeteoEvent;
import ch.hearc.meteo.tools.ImageShop;

import com.bilat.tools.reseau.rmi.RmiURL;

public class JPanelStationMeteoControlLocal extends JPanel
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public JPanelStationMeteoControlLocal(List<StationMeteoElement> listStationMeteoElements)
		{
		this.listStationMeteoElements = listStationMeteoElements;
		geometry();
		control();
		apparence();
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	public void refresh()
		{
		synchronized (this.listStationMeteoElements)
			{
			int nbSelectedStations = this.listStationMeteoElements.size();
			btnRemove.setEnabled(nbSelectedStations > 0);
			btnRemove.setText("(" + nbSelectedStations + ")");
			}
		}

	/*------------------------------*\
	|*				Set				*|
	\*------------------------------*/

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/

	private void apparence()
		{
		refresh();
		Font font = this.getFont().deriveFont(12);
		Border border = BorderFactory.createTitledBorder(BorderFactory.createLineBorder(JFrameStationMeteo.COLOR_S1, 1, true), "Gestion des stations", TitledBorder.CENTER, TitledBorder.TOP, font, JFrameStationMeteo.COLOR_BLACK);
		this.setBorder(border);
		}

	private void control()
		{
		this.btnAdd.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent arg0)
					{

					final JDialogComOptions dialogComOptions = new JDialogComOptions();
					if (dialogComOptions.getOk())
						{
							{
							Thread t = new Thread(new Runnable()
								{

									@Override
									public void run()
										{
										String stationName = dialogComOptions.getStationName();
										double stationLatitude = dialogComOptions.getStationLatitude();
										double stationLongitude = dialogComOptions.getStationLongitude();
										String stationPortName = dialogComOptions.getPortName();
										int stationSpeed = dialogComOptions.getSpeed();
										int stationDataBits = dialogComOptions.getDataBits();
										int stationParity = dialogComOptions.getParity();
										int stationStopBits = dialogComOptions.getStopBits();

										try
											{
											createPCLocal(stationName, stationLatitude, stationLongitude, stationPortName, stationSpeed, stationDataBits, stationParity, stationStopBits);
											}
										catch (UnknownHostException e)
											{
											//TODO
											}

										JPanelStationMeteoControlLocal.this.btnAdd.setIcon(new ImageIcon(ImageShop.IMAGE_ADD));
										JPanelStationMeteoControlLocal.this.btnAdd.setEnabled(true);

										}
								});
							JPanelStationMeteoControlLocal.this.btnAdd.setIcon(new ImageIcon(ImageShop.IMAGE_LOADING));
							JPanelStationMeteoControlLocal.this.btnAdd.setEnabled(false);

							t.start();
							}

						}

					}
			});
		this.btnRemove.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent e)
					{
					Thread t = new Thread(new Runnable()
						{

							@Override
							public void run()
								{
								synchronized (listStationMeteoElements)
									{
									ArrayList<StationMeteoElement> stationsToDelete = new ArrayList<StationMeteoElement>();
									for(StationMeteoElement stationMeteoElement:listStationMeteoElements)
										{

										stationsToDelete.add(stationMeteoElement);

										}
									for(StationMeteoElement stationMeteoElement:stationsToDelete)
										{
										try
											{
											stationMeteoElement.getPanelStationMeteoListElement().setVisible(false);
											stationMeteoElement.getPcLocal().getAfficheurService().disconnect();
											stationMeteoElement.getAfficheurService().disconnect();
											stationMeteoElement.getPcLocal().getMeteoService().stop();
											Thread.sleep(500);
											stationMeteoElement.getPcLocal().getMeteoService().disconnect();
											//System.out.println("disconnect");
											listStationMeteoElements.remove(stationMeteoElement);
											}
										catch (Exception e1)
											{
											e1.printStackTrace();
											}
										refresh();
										}
									}

								JPanelStationMeteoControlLocal.this.btnRemove.setIcon(new ImageIcon(ImageShop.IMAGE_DELETE));
								JPanelStationMeteoControlLocal.this.btnRemove.setEnabled(true);
								JPanelStationMeteoControlLocal.this.refresh();

								}
						});
					JPanelStationMeteoControlLocal.this.btnRemove.setIcon(new ImageIcon(ImageShop.IMAGE_LOADING));
					JPanelStationMeteoControlLocal.this.btnRemove.setEnabled(false);

					t.start();

					}
			});
		}

	private void geometry()
		{
		this.setLayout(new FlowLayout(FlowLayout.CENTER));
		btnAdd = new JButton(new ImageIcon(ImageShop.IMAGE_ADD));
		btnRemove = new JButton(new ImageIcon(ImageShop.IMAGE_DELETE));

		this.add(btnAdd);
		this.add(btnRemove);
		}

	private void createPCLocal(String stationName, double stationLatitude, double stationLongitude, String stationPortName, int stationSpeed, int stationDataBits, int stationParity, int stationStopBits) throws UnknownHostException
		{
		String serverIp = "";
		int serverPort = 0;
		Properties p = new Properties();
		try
			{
			p.load(new FileInputStream(JDialogPreferencesLocal.PROPERTY_FILENAME));
			serverIp = p.getProperty(JDialogPreferencesLocal.SERVER_IP_PROPERTY_NAME);
			serverPort = Integer.valueOf(p.getProperty(JDialogPreferencesLocal.SERVER_PORT_PROPERTY_NAME));
			}
		catch (Exception e)
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}

		RmiURL rmiURL = new RmiURL(AfficheurManager.ID_DISPLAY_MANAGER, InetAddress.getByName(serverIp), serverPort);
		ComOption comOptions = new ComOption(stationSpeed, stationDataBits, stationParity, stationStopBits);
		MeteoService_I meteoService = MeteoServiceFactory.create(stationPortName, new MeteoServiceOptions(MeteoServiceOptions.DEFAULT_DT, MeteoServiceOptions.DEFAULT_DT, MeteoServiceOptions.DEFAULT_DT), comOptions);
		try
			{
			meteoService.connect();
			}
		catch (MeteoServiceException e)
			{
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		AffichageOptions affichageOptions = new AffichageOptions(100, stationName);
		affichageOptions.setLatitude(stationLatitude);
		affichageOptions.setLongitude(stationLongitude);

		PCLocal pcLocal = new PCLocal(meteoService, meteoService.getMeteoServiceOptions(), stationName, affichageOptions, rmiURL);
		pcLocal.run();

		final AfficheurService afficheurService = (AfficheurService)AfficheurFactory.create(affichageOptions, pcLocal.getMeteoServiceWrapper());
		afficheurService.getStationMeteoElement().setPcLocal(pcLocal);

		//Ajout � la fen�tre locale
		JFrameStationMeteo.getInstance().addMeteoServiceWrapper(pcLocal.getMeteoServiceWrapper(), afficheurService);
		meteoService.addMeteoListener(new MeteoListener_I()
			{

				@Override
				public void temperaturePerformed(MeteoEvent event)
					{
					afficheurService.printTemperature(event);
					}

				@Override
				public void pressionPerformed(MeteoEvent event)
					{
					afficheurService.printPression(event);
					}

				@Override
				public void altitudePerformed(MeteoEvent event)
					{
					afficheurService.printAltitude(event);
					}
			});

		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/

	//Input
	private List<StationMeteoElement> listStationMeteoElements;

	//Tools
	private JButton btnAdd;
	private JButton btnRemove;

	}
